/*
 * Klass: UserInputQueue
 * UserInputQueue is a queue limited with locks that stores the choosen moves
 * from players.
 */

package domain;

import java.util.LinkedList;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/*******************
 * @author: Anders *
 *******************/

public class UserInputQueue{

    private LinkedList<Move> linkedQueue = new LinkedList<>();
    private Lock lock = new ReentrantLock();
    private Condition notEmpty = lock.newCondition();

    // Used for putting in a move into the queue.
    public void pushQueue(Move move){ 
        lock.lock();
        
        
        try{
            linkedQueue.addLast(move);
            
            
            notEmpty.signal();
        } finally {
            lock.unlock();
        }
    }

    // Used for getting out a move from the queue, will wait if the queue is empty.
    public Move popQueue() throws InterruptedException{ 
        lock.lock();
        
        try {
            while(linkedQueue.isEmpty()){
                notEmpty.await();
            }
            
            return linkedQueue.poll();           
        } finally {
            lock.unlock();
        }
    }
}
